<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/21 16:25
 * Desc: 注册
 */
class Service_Api_Member_Register extends Contract_Api_Member_Register
{

    public function do_reg(array $params): array
    {
        /**
         * @var $user
         * @var $pwd
         * @var $msg_code
         * @var $captcha_key
         * @var $code
         * @var $fxpcode
         * @var $company_name
         */
        extract($params);
        $msg_code_key = "reg_msgcode_send_code_{$user}";
        if (!Model_Api_V2_Common::verify_image_code($captcha_key, $code)) {
            throw new Exception(__("checkcode_error"));
        }

        if (!empty($msg_code)) {
            if (Model_Api_V2_Common::cache('get', $msg_code_key) != $msg_code) {
                throw new Exception(__("account_sms_code_error"));
            }
        }

        // 检查用户是否注册
        $member = Model_Member::member_find($user);
        if (!empty($member)) {
            throw new Exception(__("error_user_exists"));
        }


        $user_type = strpos($user, '@') !== false ? 'email' : 'phone';

        $isPhone = false;

        if ($user_type == 'phone') {
            $isPhone  = true;
            $field    = 'mobile';
            $nickname = substr($user, 0, 5) . '***';
            $reg_type = 0;
        } else {
            $nickname = $user;
            $field    = 'email';
            $reg_type  = 1;
        }

        $addtime = time();
        list($mid, $rows) = DB::insert('member', array('nickname', $field, 'jointime', 'logintime', 'pwd', 'regtype', 'company_name'))
            ->values(array($nickname, $user, $addtime, $addtime, md5($pwd), $reg_type, $company_name))
            ->execute();

        Model_Member::add_reg_from($mid, 2);

        if ($rows > 0) {

            Model_Api_V2_Common::cache('set', $msg_code_key, null);

            $member = ORM::factory('member', $mid)->as_array();

            //发送注册成功信息
            if ($isPhone) {
                St_SMSService::send_member_msg($user, NoticeCommon::MEMBER_REG_MSGTAG, $user, $pwd, "");
            } else {
                St_EmailService::send_member_email($user, NoticeCommon::MEMBER_REG_MSGTAG, $pwd, "");
            }

            //注册送积分
            $jifen = Model_Jifen::reward_jifen('sys_member_register', $mid);
            if (!empty($jifen)) {
                St_Product::add_jifen_log($mid, "注册赠送积分{$jifen}", $jifen, 2);
            }


            //是否是红包领取跳转
            $envelope_order_id = Model_Api_V2_Common::cache('get', 'envelope_order_id');
            if ($envelope_order_id) {
                Model_Api_V2_Common::cache('set', 'envelope_new_member', 1);
            }

            Model_Api_V2_Common::cache('set', 'envelope_order_id', null);
            Plugin_Core_Factory::factory()->add_listener('on_member_register', $member)->execute();

            // 检查是否安装分销商应用
            if (!empty($fxpcode)) {
                if (St_Functions::is_normal_app_install('mobiledistribution')) {
                    // 检查是否是分销商
                    $is_fenxiao = DB::select('fxcode', 'status')
                        ->from('fenxiao')
                        ->where('memberid', '=', $mid)->execute()
                        ->current();
                    if (!$is_fenxiao) { // 不是分销商且分销商fxcode不为空
                        Model_Fenxiao::invite($mid, $fxpcode);
                    }
                }
            }

            // 验证成功后清除图片缓存
            Model_Api_V2_Common::cache('set', $captcha_key, null);

            Model_Member_Grade::new_user_gift($member);

            //登录状态 $insertId
            return Model_Api_V2_Member_Login::login_init($member);
        } else {
            throw new Exception(__("error_member_insert"));
        }
    }
}